# -*- coding: utf-8 -*-
# @Time    : 2022/4/7 15:48
# @Author  : zyw
# @Email   : panda_kings@163.com
# @File    : middleware.py
from flask import g,request,redirect,current_app


from common.utils.myjwt import verify_token


# 使用before_request装饰,每个请求都要登录后才能获取
def jwt_authentication():
    # 用来存放全局的用户信息
    g.account = None
    g.user_id = None
    g.is_refresh = False
    # 从request中获取token
    token = request.headers.get('Authorization')
    print("token>>>>", token)
    if token is not None and token.startswith('JWT '):
       # 获取到token
       # 解析payload,获取到token 中的用户xinix
        payload = verify_token(token)
        if payload is not None:
            # 把载荷信息保存在g对象
            # token 中是: account 到期时间
            # refresh token是: account,有效期,is_refresh
            g.account = payload.get('account')
            g.user_id = payload.get('user_id')
            g.is_refresh = payload.get('is_refresh', False)




